import commands
import utils
import os

def zipfile(indir, outfile):
	cmdzip = 'gzip -r -c ' + indir + ' > ' + outfile
	commands.getoutput(cmdzip)
	return os.path.getsize(outfile)


def zipdir(indir, grep, outdir, mode = 'w'):
	results = utils.lsgrep(indir, grep)
	emptyfile = open(outdir + '/' + 'emptyfile', mode)
	availablefile = open(outdir + '/' + 'availablefile', mode)
	for result in results:
		size = zipfile(indir + '/' + result, outdir + '/' + result)
		if size == 0:
			emptyfile.write(result + '\n')
		else:
			availablefile.write(result + '\n')


def uploadB1(number, desdir = 'urlcrawl:/space/webcrawl-malware/data/JSAdd2Tree/'):
	infile = '../../data/TempData/URLs/alexaustop'
	uploaddir = '../../data/JSAdd2Tree/'
	inf = open(infile, 'r')
	count = 0
	for line in inf:
		if count >= number:
			break
		else:
			count = count + 1
		uploadfile = 'B1.' + line.split(',')[0][2:-1]
		cmdupload = 'scp -r ' + uploaddir + uploadfile + ' ' + desdir
		commands.getoutput(cmdupload)


def uploadKthB1(kth, desdir = 'urlcrawl:/space/webcrawl-malware/data/JSAdd2Tree/'):
	infile = '../../data/TempData/URLs/alexaustop'
	uploaddir = '../../data/JSAdd2Tree/'
	inf = open(infile, 'r')
	count = 0
	for line in inf:
		if count != kth-1:
			count = count + 1
			continue
		else:
			uploadfile = 'B1.' + line.split(',')[0][2:-1]
			cmdupload = 'scp -r ' + uploaddir + uploadfile + ' ' + desdir
			commands.getoutput(cmdupload)
			return



def uploadM(number, desdir = 'urlcrawl:/space/webcrawl-malware/data/JSAdd2Tree/'):
	infile = '../../data/JSZipped/availablefile'
	uploaddir = '../../data/JSAdd2Tree/'
	inf = open(infile, 'r')
	count = 0
	for line in inf:
		if count >= number:
			break
		else:
			count = count + 1
		uploadfile = line[:-1]
		cmdupload = 'scp -r ' + uploaddir + uploadfile + ' ' + desdir
		print cmdupload
		commands.getoutput(cmdupload)



def getJSHash(instance):
	return path_checksum(instance)



if __name__=="__main__":
	zipdir('../../data/JSAdd2Tree/', 'MTerry', '../../data/JSZipped')
	# zipdir('../../data/JSAdd2Tree/', 'M7', '../../data/JSZipped', 'a')
	# uploadM(13)
	# uploadB1(56, 'urlcrawl:/space/webcrawl-malware/data/AccurateJSAdd2Tree/')
	# uploadKthB1(59, 'urlcrawl:/space/webcrawl-malware/data/AccurateJSAdd2Tree/')
	# uploadKthB1(53)
